﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skyhook.Core;
using Skyhook.Resources;

namespace Skyhook.Logic
{
    static class VisualsFactory
    {
        const string VisualPrefix = "logic.visuals.visual";
        const string ImageOption = "image";
        const string GroupsNumberOption = "groupsnumber";
        const string LayerOption = "layer";
        const string WidthOption = "width";
        const string HeightOption = "height";

        public static IVisualObject CreateCommonVisual(string assetname)
        {
            string imagename = null;//GameConfiguration.GetOptionValue<string>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, ImageOption), "");
            ImageCollection image = ResourceFactory.CreateFromFile<ImageCollection>(imagename);

            int groupsnum = 0;//GameConfiguration.GetOptionValue<int>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, GroupsNumberOption), 1);
            string layername = null;// GameConfiguration.GetOptionValue<string>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, LayerOption), "");

            int[] groupnums = new int[groupsnum * 3];
            int framespergroup = image.FramesCount / groupsnum;
            for (int i = 0; i < groupsnum; i++)
            {
                groupnums[i * 3] = i;
                groupnums[i * 3 + 1] = i * framespergroup;
                groupnums[i * 3 + 2] = framespergroup;
            }
            
            IVisualObject visual = VisualsEngine.CreateVisualObject(assetname, image, groupnums, layername);

            return visual;
        }

        public static IVisualObject CreatePartialVisual(string assetname,params int[] groups)
        {
            string imagename = null;//GameConfiguration.GetOptionValue<string>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, ImageOption), "");
            ImageCollection image = ResourceFactory.CreateFromFile<ImageCollection>(imagename);

            int groupsnum = 0;//GameConfiguration.GetOptionValue<int>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, GroupsNumberOption), 1);
            string layername = null;//GameConfiguration.GetOptionValue<string>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, LayerOption), "");

            int[] groupnums = new int[groups.Length * 3];
            int framespergroup = image.FramesCount / groupsnum;
            for (int i = 0; i < groups.Length; i++)
            {
                groupnums[i * 3] = groups[i];
                groupnums[i * 3 + 1] = groups[i] * framespergroup;
                groupnums[i * 3 + 2] = framespergroup;
            }

            string resultname = assetname;
            for (int i = 0; i < groups.Length; i++)
            {
                resultname += "_" + groups[i].ToString();
            }
            IVisualObject visual = VisualsEngine.CreateVisualObject(resultname, image, groupnums, layername);

            return visual;
        }

        public static IVisualObject CreateHugeLeftPartVisual(string assetname, params int[] groups)
        {
            string imagename = null;// GameConfiguration.GetOptionValue<string>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, ImageOption), "");
            ImageCollection image = ResourceFactory.CreateFromFile<ImageCollection>(imagename);

            int groupsnum = 0;// GameConfiguration.GetOptionValue<int>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, GroupsNumberOption), 1);
            string layername = null;// GameConfiguration.GetOptionValue<string>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, LayerOption), "");

            int[] groupnums = new int[groups.Length * 3];
            int framespergroup = image.FramesCount / groupsnum;
            for (int i = 0; i < groups.Length; i++)
            {
                groupnums[i * 3] = groups[i];
                groupnums[i * 3 + 1] = groups[i] * framespergroup;
                groupnums[i * 3 + 2] = framespergroup;
            }

            string resultname = assetname;
            for (int i = 0; i < groups.Length; i++)
            {
                resultname += "_" + groups[i].ToString();
            }
            resultname += "left";
            IVisualObject visual = VisualsEngine.CreateVisualObject(resultname, image, groupnums, layername,VisualsEngine.CreateVisualRenderer(0));

            return visual;
        }

        public static IVisualObject CreateHugeRightPartVisual(string assetname, params int[] groups)
        {
            string imagename = null;// GameConfiguration.GetOptionValue<string>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, ImageOption), "");
            ImageCollection image = ResourceFactory.CreateFromFile<ImageCollection>(imagename);

            int groupsnum = 0;// GameConfiguration.GetOptionValue<int>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, GroupsNumberOption), 1);
            string layername = null;// GameConfiguration.GetOptionValue<string>(GameConfiguration.MakeOptionName(VisualPrefix, assetname, LayerOption), "");

            int[] groupnums = new int[groups.Length * 3];
            int framespergroup = image.FramesCount / groupsnum;
            for (int i = 0; i < groups.Length; i++)
            {
                groupnums[i * 3] = groups[i];
                groupnums[i * 3 + 1] = groups[i] * framespergroup;
                groupnums[i * 3 + 2] = framespergroup;
            }

            string resultname = assetname;
            for (int i = 0; i < groups.Length; i++)
            {
                resultname += "_" + groups[i].ToString();
            }
            resultname += "right";
            IVisualObject visual = VisualsEngine.CreateVisualObject(resultname, image, groupnums, layername, VisualsEngine.CreateVisualRenderer(1));

            return visual;
        }
    }
}
